package com.zzh.ytapiinterface.controller;


import cn.hutool.Hutool;
import cn.hutool.core.codec.Morse;
import cn.hutool.core.date.*;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.zzh.demo.modal.User;
import com.zzh.ytapiinterface.modal.Soul;
import com.zzh.ytapiinterface.service.SoulService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author yegen
 * @create -02-03 11:05
 */
@RestController
@RequestMapping("/yt")
public class NameController {

    @Value("${ytapi.path}")
    private String basePath;

    @Resource
    private SoulService soulService;

    @GetMapping("/")
    public String getNameByGet(String name) {
        return "Get 你的名字是"+name;
    }

    @PostMapping("/")
    public String getNameByPost(@RequestParam String name) {
        return "Post 你的名字是"+name;
    }

    @PostMapping("/user")
    public String getUsernameByPost(@RequestBody User user, HttpServletRequest request) {

        return "Post 用户名字是❤"+user.getUsername()+"❤";
    }

    @PostMapping("/soul")
    public String getSoulByPost(@RequestBody User user, HttpServletRequest request) {
        int i = RandomUtil.randomInt(11, 19999);

        Soul soul = soulService.getById(i);


        return "💛"+soul.getTitle()+"💛";
    }

    @PostMapping("/soulT")
    public String getSoulTByPost(@RequestBody User user, HttpServletRequest request) {

        int i = RandomUtil.randomInt(11, 19999);
        String s = i+"";
        QueryWrapper<Soul> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hits", s);



        Soul soul = soulService.getOne(queryWrapper);


        return "💛"+soul.getTitle()+"💛";
    }

//    todo hutool  mosimima

    @PostMapping("/zxing")
    public String getZxingByPost(@RequestBody User user, HttpServletRequest request) {
        String name = user.getUsername();

        QrConfig config = new QrConfig();
        String s = RandomUtil.randomString(8);
// 高纠错级别
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        config.setImg(basePath+"YT.jpg");
        QrCodeUtil.generate(
                name, //二维码内容
                config,
                FileUtil.file(basePath+s+".jpg")//写出到的文件
        );
        return "💛"+"http://47.96.143.109/erweima/"+s+".jpg"+"💛";
    }


    @PostMapping("/morse")
    public String getMorseByPost(@RequestBody User user, HttpServletRequest request) {
        final Morse morseCoder = new Morse();

        String text = user.getUsername();

        return "💛"+morseCoder.encode(text)+"💛";
    }

    @PostMapping("/decodemorse")
    public String getDecodeMorseByPost(@RequestBody User user, HttpServletRequest request) {
        final Morse morseCoder = new Morse();

        String text = user.getUsername();

        return "💛" + morseCoder.decode(text) + "💛";

    }
    @PostMapping("/nong")
    public String getChineseDateByPost(@RequestBody User user, HttpServletRequest request) {

        ChineseDate date = new ChineseDate(DateUtil.parseDate(user.getUsername()));

        date.getFestivals();
// 庚子鼠年 正月初一

        date.toString();

        // 庚子年甲申月癸卯日
        String cyclicalYMD = date.getCyclicalYMD();

        return "💛天干地支：" + cyclicalYMD+"  农历："+date.toString()+"  节日："+date.getFestivals() + "💛";

    }

    @PostMapping("/zodiac")
    public String getZodiacByPost(@RequestBody User user, HttpServletRequest request) {
        String dateStr = user.getUsername();
        Date date = DateUtil.parse(dateStr);
        DateTime dateTime = new DateTime(date);

        // "摩羯座"
        String zodiac = DateUtil.getZodiac(DateUtil.month(date), dateTime.dayOfMonth());

        // "狗"
        String chineseZodiac = DateUtil.getChineseZodiac(DateUtil.year(date));

        return "💛年龄："+ DateUtil.ageOfNow(dateStr) +" 星座："+zodiac+"  属"+chineseZodiac+ "💛";

    }
}
